package com.easybuy.controller;

import com.easybuy.pojo.UserAddress;
import com.easybuy.service.UserAddressService.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
//关于地址的操作
public class Member_UserAddressController {
    @Resource
    UserAddressService userAddressService;

    @RequestMapping("/UserAddressList")
    @ResponseBody
    //查询全部地址信息
    public List<UserAddress> UserAddressList(){
        return userAddressService.UserAddressList();
    }


    @RequestMapping("/getAddress")
    @ResponseBody
    //查询一个地址信息
    public UserAddress getAddress(@RequestParam int id){
        return userAddressService.getUserAddressById(id);
    }


    @RequestMapping("/getUserAddress")
    @ResponseBody
    //查询用户地址信息
    public List<UserAddress> getUserAddress(@RequestParam int userId){
        List UserAddressList=userAddressService.getUserAddress(userId);
        return UserAddressList;
    }


    @RequestMapping("/modifyUserAddress")
    @ResponseBody
    //修改地址信息
    public Boolean modifyUserAddress(UserAddress userAddress){
        if(userAddressService.modifyUserAddress(userAddress)>0){
            return true;
        }else{
            return false;
        }
    }


    @RequestMapping("/isDefaultUserAddress")
    @ResponseBody
    //设置默认功能（设置地址默认）
    public boolean isDefaultUserAddress(@RequestParam Integer id,@RequestParam Integer userId){
        if (userAddressService.cleanDefaultUserAddress(userId)>0&&userAddressService.isDefaultUserAddress(id)>0){
            return true;
        }else {
            return false;
        }
    }


    @RequestMapping("/addUserAddress")
    @ResponseBody
    //增加地址
    public Boolean addUserAddress(UserAddress userAddress){
        if(userAddressService.addUserAddress(userAddress)>0){
            return true;
        }else{
            return false;
        }
    }


    @RequestMapping("/deleAddress")
    @ResponseBody
    //删除地址
    public Boolean deleAddress(int id){
        if(userAddressService.deleAddress(id)>0){
            return true;
        }else{
            return false;
        }
    }
}
